package cn.wolfcode.shop.cloud.web.controller;


import cn.wolfcode.shop.cloud.Result;
import cn.wolfcode.shop.cloud.service.ISeckillGoodService;
import cn.wolfcode.shop.cloud.service.RedisService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.vo.SeckillGoodVO;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/seckillGood")
public class SeckillGoodController {

    @Autowired
    ISeckillGoodService seckillGoodService;

    @Autowired
    RedisService redisService;

    @RequestMapping("/query")
    public Result query(){
        return Result.success(seckillGoodService.queryByCache());
    }

    @RequestMapping("/find")
    public Result find(Long seckillId){
        return Result.success(seckillGoodService.findByCache(seckillId));
    }

    @RequestMapping("/initData")
    public Result<String> initData(){
        List<SeckillGoodVO> list = seckillGoodService.query();
        for (SeckillGoodVO seckillGoodVO : list) {
            //库存
            redisService.set(SeckillKeyPrefix.SECKILL_STOCK,seckillGoodVO.getId()+"",seckillGoodVO.getStockCount());
            //把秒杀商品也放入到redis当中
            redisService.hset(SeckillKeyPrefix.SECKILL_GOOD,"",seckillGoodVO.getId()+"",seckillGoodVO);
        }
        return Result.success("初始化成功");
    }

   /* @RequestMapping("/query")
    @ResponseBody
    public Result<List<SeckillGoodVO>> query(){
        return Result.success(seckillService.queryByCache());
    }*/

/*    @RequestMapping("/find")
    @ResponseBody
    public Result<SeckillGoodVO> find(Long seckillId){
        return Result.success(seckillService.find(seckillId));
    }*/
}
